﻿using Dapper;
using System;
using BCEntity.OA.WorkflowActive.WorkflowActivityStepFormItem;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowActive.WorkflowActivityStepFormItem
{
    /// <summary>
    /// 工作流活动步骤表单项
    /// </summary>
    public class WorkflowActivityStepFormItemData : IWorkflowActivityStepFormItemData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowactivitystepformitem";
        private const string QUERY = @"SELECT WorkflowActivityStepFormItemId,WorkflowActivityStepFormConfigId,FormItemCode,FormDatabase,FormTable,BoundField,ItemTitle,ItemHeight,ItemOrder,
                                            ItemType,ItemValue,WorkflowActivityId,PreviewType,SettingElements,CreateAdminId,CreateAdminName,CreateTime,EditAdminId,EditAdminName,UpdateTime,
                                            SubmitRole,TemplateCode,TemplateName,StepCode,WorkflowCode,WorkflowName,FormConfigTitle,FormConfigCode 
                                        FROM oa_workflowactivitystepformitem";
        public WorkflowActivityStepFormItemData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查工作流活动步骤表单项是否存在
        public async Task<bool> Exists(long workflowActivityStepFormItemId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowActivityStepFormItemId=?WorkflowActivityStepFormItemId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityStepFormItemId", workflowActivityStepFormItemId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加工作流活动步骤表单项
        public async Task<WorkflowActivityStepFormItemEntity> Add(WorkflowActivityStepFormItemEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("oa_workflowactivitystepformitem", new string[] { "WorkflowActivityStepFormConfigId", "FormItemCode", "ItemTitle", "ItemHeight", 
                "ItemOrder", "ItemType", "ItemValue", "WorkflowActivityId", "PreviewType", "SettingElements", "CreateAdminId", "CreateAdminName", "SubmitRole", "TemplateCode",
                "TemplateName", "StepCode", "WorkflowCode", "WorkflowName", "FormConfigTitle", "FormConfigCode","FormDatabase","FormTable","BoundField"});
            sql = string.Format("{0} {1} WHERE WorkflowActivityStepFormItemId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityStepFormConfigId", entity.WorkflowActivityStepFormConfigId, DbType.Int64, size: 20);
            parameters.Add("?FormItemCode", entity.FormItemCode?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?ItemTitle", entity.ItemTitle?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ItemHeight", entity.ItemHeight?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?ItemOrder", entity.ItemOrder, DbType.Int32, size: 11);
            parameters.Add("?ItemType", entity.ItemType?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ItemValue", entity.ItemValue?.Trim(), DbType.String);
            parameters.Add("?WorkflowActivityId", entity.WorkflowActivityId, DbType.Int64, size: 20);
            parameters.Add("?PreviewType", entity.PreviewType?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SettingElements", entity.SettingElements?.Trim(), DbType.String);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?SubmitRole", entity.SubmitRole?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?StepCode", entity.StepCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?WorkflowCode", entity.WorkflowCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?WorkflowName", entity.WorkflowName?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormDatabase", entity.FormDatabase?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormTable", entity.FormTable?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?BoundField", entity.BoundField?.Trim(), DbType.AnsiString, size: 100);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowActivityStepFormItemEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改工作流活动步骤表单项
        public async Task<WorkflowActivityStepFormItemEntity> Update(WorkflowActivityStepFormItemEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("oa_workflowactivitystepformitem", new string[] { "WorkflowActivityStepFormConfigId", "FormItemCode", "FormDatabase", "FormTable",
                "BoundField", "ItemTitle", "ItemHeight", "ItemOrder", "ItemType", "ItemValue", "WorkflowActivityId", "PreviewType", "SettingElements", "EditAdminId", "EditAdminName", 
                "SubmitRole", "TemplateCode", "TemplateName", "StepCode", "WorkflowCode", "WorkflowName", "FormConfigTitle", "FormConfigCode" ,"FormDatabase","FormTable","BoundField"
            }, new string[] { "WorkflowActivityStepFormItemId" });
            sql = string.Format("{0} {1} WHERE WorkflowActivityStepFormItemId=?WorkflowActivityStepFormItemId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityStepFormItemId", entity.WorkflowActivityStepFormItemId, DbType.Int64, size: 20);
            parameters.Add("?WorkflowActivityStepFormConfigId", entity.WorkflowActivityStepFormConfigId, DbType.Int64, size: 20);
            parameters.Add("?FormItemCode", entity.FormItemCode?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormDatabase", entity.FormDatabase?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormTable", entity.FormTable?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?BoundField", entity.BoundField?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?ItemTitle", entity.ItemTitle?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ItemHeight", entity.ItemHeight?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?ItemOrder", entity.ItemOrder, DbType.Int32, size: 11);
            parameters.Add("?ItemType", entity.ItemType?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ItemValue", entity.ItemValue?.Trim(), DbType.String);
            parameters.Add("?WorkflowActivityId", entity.WorkflowActivityId, DbType.Int64, size: 20);
            parameters.Add("?PreviewType", entity.PreviewType?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?SettingElements", entity.SettingElements?.Trim(), DbType.String);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?SubmitRole", entity.SubmitRole?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?StepCode", entity.StepCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?WorkflowCode", entity.WorkflowCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?WorkflowName", entity.WorkflowName?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormDatabase", entity.FormDatabase?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?FormTable", entity.FormTable?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?BoundField", entity.BoundField?.Trim(), DbType.AnsiString, size: 100);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowActivityStepFormItemEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除工作流活动步骤表单项
        public async Task<bool> Delete(long workflowActivityStepFormItemId)
        {
            string sql = @"DELETE FROM oa_workflowactivitystepformitem WHERE WorkflowActivityStepFormItemId=?WorkflowActivityStepFormItemId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityStepFormItemId", workflowActivityStepFormItemId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取工作流活动步骤表单项
        public async Task<WorkflowActivityStepFormItemEntity> GetEntity(long workflowActivityStepFormItemId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowActivityStepFormItemId=?WorkflowActivityStepFormItemId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityStepFormItemId", workflowActivityStepFormItemId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityStepFormItemEntity>(sql, parameters);
        }
        #endregion

        #region 获取工作流活动步骤表单项列表
        public async Task<IEnumerable<WorkflowActivityStepFormItemEntity>> GetList(long workflowActivityId)
        {
            string sql = string.Format("{0} WHERE WorkflowActivityId=?WorkflowActivityId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityId", workflowActivityId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<WorkflowActivityStepFormItemEntity>(sql, parameters);
        }
        #endregion

        #region 获取工作流活动步骤表单项分页
        public async Task<(IEnumerable<WorkflowActivityStepFormItemEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowactivitystepformitem {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowActivityStepFormItemEntity, int>(sql, parameters);
        }
        #endregion
    }
}



